<?php
namespace PcHome\Lib\TagLib;
use Think\Template\TagLib;
use PcHome\Service\CmsService;
use Common\Lib\Think\StringUtil;
use Admin\Controller\SettingController;
class XiaMi extends TagLib {

    protected $tags = array(
    	'global' => array('attr'=>'name','close'=>0),  //全局标签
        'field'  => array('attr'=>'name','close'=>0),  //字段标签
        'flink'  => array('attr'=>'row','level'=>2),  //友情链接标签
        'nav' => array('attr'=>'type,typeid,row,parentid,col,currentstyle','level'=>2), //导航栏目标签
        'arclist' => array('attr'=>'type,typeid,row,titlelen,infolen,orderby','level'=>2), //导航栏目标签
    );

    protected $mapping = array(
        'cfg_webname'     => 'site_name',      //网站名称
        'cfg_cmsurl'      => 'site_host',      //网站名称
        'cfg_seo_webname' => 'site_seo_title', //网站SEO名称
        'cfg_keywords'    => 'site_seo_keywords', //网站SEO关键词
        'cfg_description' => 'site_seo_description', //网站SEO描述
        'cfg_powerby'     => 'site_copyright', //网站版权
        'cfg_beian'       => 'site_icp',      //网站备案号
    );

    /*
     * 全局标签(搞定)
     * */
    public function _global($tag,$content) {
    	$name = !empty($tag['name'])?$tag['name']:'';
        $field = !empty($this->mapping[$name])?$this->mapping[$name]:'';
        if(empty($field)) {
            return '';
        }
        $setting = D("Common/Setting")->getSettingByKey(SettingController::SITE_KEY);
        return  empty($setting[$field]) ? '' : $setting[$field];
    }

    /*
     * 友情链接标签(搞定)
     * */
    public function _flink($tag, $content) {
        $limit = empty($tag['row']) ? 24 : intval($tag['row']);
        $titlelen = empty($tag['titlelen']) ? 24 : intval($tag['titlelen']);
        $rows = CmsService::getFlink($limit);
        $revalue = '';
        foreach ($rows as $row) {
            $rbtext = preg_replace("/\[field:url([\/\s]{0,})\]/isU", $row['url'], $content);
            $rbtext = preg_replace("/\[field:webname([\/\s]{0,})\]/isU", StringUtil::msubstr($row['webname'], 0, $titlelen), $rbtext);
            $revalue .= $rbtext;
        }
        return $revalue;
    }

    /*
     * 字段标签（搞定）
     * */
    public function _field($tag, $content) {
        $name = !empty($tag['name'])?$tag['name']:'';
        $value = '';
        switch ($name) {
            case 'templeturl': //模板路径
                $value = TMPL_PATH;
                break;
            case 'title': //文章标题
            case 'pubdate': //发布时间
            case 'source':  //文章来源
            case 'writer':  //文章作者
            case 'description': //文章摘要
            case 'body': //文章主体
            case 'pagebreak': //内容分页
                break;
            default:
                break;
        }
         return $value;
    }

    /*
     * 导航栏目标签(搞定)
     * */
    public function _nav($tag, $content) {
        $type   = empty($tag['type']) ? 'son': $tag['type'];
        $navid = empty($tag['navid']) ? 0 : $tag['navid'];
        $parentid   = empty($tag['parentid']) ? 0 : $tag['parentid']; //父级
        $currentstyle = empty($tag['currentstyle']) ? '' : $tag['currentstyle'];
        $limit  = empty($tag['row']) ? 100 : $tag['row'];

        //没有填写栏目id
        if ($navid != 0 && empty($parentid) && $type=='self') {
            $row = CmsService::getMainChannel(array('nav_id' => $navid),1);
            $parentid = $row[0]['parent_id'];
        }

        $where = array();
        if ($type=='top') {
            $where['parent_id'] = 0;
        } elseif ($type=='son') {
            $where['parent_id'] = $navid;
        } elseif ($type=='self') {
            $where['parent_id'] = $parentid;
        }

        $rows = CmsService::getMainNav($where, $limit);
        $revalue = '';
        foreach ($rows as $row) {
            $rbtext = preg_replace("/\[field:typelink([\/\s]{0,})\]/isU", $row['href'], $content);
            $rbtext = preg_replace("/\[field:typename([\/\s]{0,})\]/isU", $row['nav_name'], $rbtext);
            $revalue .= $rbtext;
        }
        return $revalue;
    }

    /*
     * 文档列表(基本搞定)
     * */
    public function _arclist($tag, $content) {
        $typeid = empty($tag['typeid']) ? I('get.typeid', '1','intval') : $tag['typeid'];
        $limit = empty($tag['row']) ? 24 : $tag['row'];
        $titlelen = empty($tag['titlelen']) ? 24 : $tag['titlelen'];
        $infolen = empty($tag['infolen']) ? 80 : $tag['infolen'];
        $orderby = empty($tag['orderby']) ? '' : $tag['orderby'];

        $where = array();
        $where['article_type'] = $typeid;

        $rows = CmsService::getArcist($where, $limit, $orderby);
        $revalue = '';
        foreach ($rows as $row) {
            $rbtext = preg_replace("/\[field:arcurl([\/\s]{0,})\]/isU", $row['article_id'], $content);
            $rbtext = preg_replace("/\[field:title([\/\s]{0,})\]/isU",  StringUtil::msubstr($row['title'], 0, $titlelen), $rbtext);
            if(preg_match("/\[field:infos([\/\s]{0,})\]/isU", $content)) {
                $rbtext = preg_replace("/\[field:infos([\/\s]{0,})\]/isU",  StringUtil::msubstr($row['content'], 0, $infolen), $rbtext);
            }
            $revalue .= $rbtext;
        }
        return $revalue;
    }


}

